<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑收货地址</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1" />
    <link rel="stylesheet" href="{{ url('/common/mobile-common/common.css') }}">
    <link rel="stylesheet" href="{{ url('mobile/css/member/addaddress.css') }}">
    <script src="{{ url('mobile/js/jquery-1.8.3.min.js') }}"></script>
    <script src="{{ url('/common/mobile-common/hmt.js') }}"></script>
</head>
<body>
<div class="abouts">
    <div class="wrap-cont clearfix">
        <!--添加新地址-->
        <div class="wrap-cont address-add">
            <div class="secti add-ys">
                <p>
                    <span>收货人:</span>
                    <input type="text" name="nameUser" value="{{$one->name}}">
                </p>
                <p>
                    <span>联系电话:</span>
                    <input type="text" name="telNum" value="{{$one->phone}}">
                </p>
                <p class="areas">
                    <span>所在省:</span>
                    <select class="province" name="province" value="{{$one->province}}">
                        <option>请选择所在省</option>
                        @foreach($areas as $area)
                            <option city="{{ collect($area['city'])->toJson() }}">{{ $area['name'] }}</option>
                        @endforeach
                    </select>
                </p>
                <p class="stres">
                    <span>所在市:</span>
                    <select class="city" name="city">
                        <option>请选择所在市</option>
                    </select>
                </p>
                <p class="addressTxt">
                    <span>详细地址:</span>
                    <input type="text" name="addRess" value="{{$one->address}}" >
                </p>
            </div>
            <div class="secti set-mr">
                <p>
                    <span>设为默认:</span>
                    <em><input type="checkbox" name="sequence" checked="checked">
                        <img src="{{ url('mobile/images/check.png') }}">
                    </em>
                </p>
            </div>
            <div class="secti opt-btn">
                <a class="save-btn">保存</a>
            </div>
        </div>



    </div>
</div>
</body>
<script>
    $(function(){
        //地区选择
        function getCity() {
            var city = $('.province').find('option:selected').attr('city');
            city = JSON.parse(city);
            var option = '';
            $('.city').empty();
            $(city).each(function () {
                option += '<option>' + this.name + '</option>';
            });
            $('.city').append(option);
        }

        $('.province').on('change',function(){
            getCity();
        });
        $('.province').find('option').each(function(){
            if($(this).text() == "{{$one->province}}"){
                $(this).attr('selected','selected');
                getCity();
            }
        });
        $('.city').find('option').each(function(){
            if($(this).text() == "{{$one->city}}"){
                $(this).attr('selected','selected');
            }
        });

        //添加地址页面新建地址保存
        $(".save-btn").click(function(){
            var myreg = /^1[34578]\d{9}$/;
            var nameuser=$(".add-ys p input[name='nameUser']").val();
            var telNum=$(".add-ys p input[name='telNum']").val();
            var province=$(".province option:selected").val();
            var city=$(".city option:selected").val();
            var addRess=$(".add-ys p input[name='addRess']").val();
            if ($("[name=sequence]").is(':checked')) {
                var sequence = 1;
            } else {
                var sequence = 2;
            }

            if(nameuser==''){
                alert("请输入姓名");
                $(".add-ys p input[name='nameUser']").focus();
            }
            else if(telNum==''){
                alert("请输入手机号码");
                $(".add-ys p input[name='telNum']").focus();
            }
            else if(!telNum.match(myreg)){
                alert("手机格式不正确！");
                $(".add-ys p input[name='telNum']").focus();
            }
            else if(province==0){
                alert("请选择省");
            }
            else if(city==0){
                alert("请选择市");
            }
            else if(addRess==''){
                alert("请填写详细地址");
                $(".add-ys p input[name='addRess']").focus();
            }
//			else {
//				$("header .top p").text("管理收货地址");
//				alert("地址添加成功");
//				$(".wrap-cont.address-show").show();
//				$(".wrap-cont.address-add").hide();
//			}
            else{
                $.ajax({
                    type:'post',
                    url:"{{ url('wap/receiver/edit/'.$one->id) }}",
                    data:{name:nameuser,phone:telNum,province:province,city:city,address:addRess,sequence:sequence},
                    success:function(data){
                        if (data.success) {
                            window.location.href="{{url('wap/receiver')}}";
                        }
                    }
                });
            }


             //复选框样式
     $("input[type='checkbox']").click(function(){
            var checkStatus=($(this).attr("checked"));
            if(checkStatus=='checked'){
                $(this).parent().find("img").attr("src","{{ url('mobile/images/check.png') }}");
            }
            else
            {
                $(this).parent().find("img").attr("src","{{ url('mobile/images/no-check.png') }}");
            }
     });
        });
    })
</script>
</html>
